package zsc.ruanc.practicaltraining.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zsc.ruanc.practicaltraining.exception.ApiException;
import zsc.ruanc.practicaltraining.mapper.ComplaintMapper;
import zsc.ruanc.practicaltraining.mapper.UserMapper;
import zsc.ruanc.practicaltraining.model.domain.TComplaint;
import zsc.ruanc.practicaltraining.model.domain.TUser;
import zsc.ruanc.practicaltraining.model.vo.TimeVo;
import zsc.ruanc.practicaltraining.model.vo.handleVo;
import zsc.ruanc.practicaltraining.service.ComplaintService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    private ComplaintMapper complaintMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     *  用户投诉
     * @param complaint
     * @param userId  投诉者的用户id
     * @return
     */
    @Transactional
    public boolean insert(TComplaint complaint, int userId) {

        if (complaint.getToId() != null) { // 前端传了被投诉者的id
            // 检查人头是否存在
            if (!userMapper.checkUser(complaint.getToId())) {
                throw new ApiException(-5, "你在投诉空气吗");
            }
        } else if (complaint.getToName() != null) { // 前端传了被投诉者的用户名
            TUser user = userMapper.selectByName(complaint.getToName());
            if (user == null) {
                throw new ApiException(-5, "你在投诉空气吗");
            }
            complaint.setToId(user.getId());
        } else { // 什么都没传
            throw new ApiException(-5, "你在投诉空气吗");
        }





        complaint.setCreated(new Date());
        complaint.setFromId(userId);
        int x = complaintMapper.insert(complaint);

        return true;

    }

    // 用户删除投诉
    @Override
    public boolean delete(int id, int userId) {
        int x = complaintMapper.delete(id, userId);
        if (x == 0) {
            throw new ApiException(404);
        }
        return true;
    }

    // 用户查看自己的投诉列表
    @Override
    public List<TComplaint> select(int userId) {
        return complaintMapper.select(userId);
    }

    // 用户修改投诉信息 如果管理员已经处理完毕则不能修改
    @Override
    public boolean update(TComplaint complaint, int userId) {
        complaint.setFromId(userId);
        int x = complaintMapper.update(complaint);

        if (x == 0) {
            throw new ApiException(-1, "更新失败，检查信息是否有误或者管理员已处理");
        }

        return true;
    }


    @Override
    public int getFromId(int id) {
        return complaintMapper.selectFromId(id);
    }


    // -----------管理员权限--------------------------------

    @Override
    public Map adminSelect(TimeVo vo) {

        PageHelper.startPage(vo.getCur(), vo.getSize());
        PageInfo<TComplaint> pageInfo = new PageInfo<>(this.complaintMapper.adminSelect(vo));

        Map map = new HashMap();
        map.put("total", pageInfo.getTotal());
        map.put("list", pageInfo.getList());

        return map;
    }

    /**
     *
     * @ id
     * @ toId
     * @ result
     * @ flag 如果管理员觉得已经解决问题，把flag改为true 则用户在不能修改投诉内容
     * @return
     */
    @Override
    public boolean handle(handleVo vo) {

        vo.setResultTime(new Date());
        boolean x = complaintMapper.handle(vo);

        if (!x) {
            throw new ApiException(404, "可能用户已撤销投诉");
        }
        return true;

    }




}
